<?php
class Product extends Vfriend_Db {	
	var $cache;
	var $clear;
	public function __construct() {
		$this->cache = new Vfriend_Cache();
		$this->clear = $this->cache->clear;
	}
	public function get($intProductId) {
		try {		
			$strKey 	= PRODUCT_GET . $intProductId;
			$arrResult	= $this->cache->get($intProductId);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call products_get(?)');
				$this->bindParam(1, $intProductId, PDO::PARAM_INT, 11);
				$arrResult 	= $this->fetchAll();
				$this->cache->set($strKey, $arrResult, PRODUCT_GET_EXPIRE);
			}
			return isset($arrResult[0]) ? $arrResult[0] : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Product::get - ' .$e->getMessage());			
			return null;
		}
	}
	public function getName($intProductId) {
		try {		
			$strKey 	= PRODUCT_GETNAME . $intProductId;
			$arrResult	= $this->cache->get($intProductId);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call products_get_name(?)');
				$this->bindParam(1, $intProductId, PDO::PARAM_INT, 11);
				$arrResult 	= $this->fetchAll();
				$this->cache->set($strKey, $arrResult, PRODUCT_GETNAME_EXPIRE);
			}
			return isset($arrResult[0]['name']) ? $arrResult[0]['name'] : '';
		} catch(Exception $e) {
			Vfriend_Function::logDb('Product::getName - ' .$e->getMessage());			
			return null;
		}
	}
	public function getAll($intCate, $intSub, $intType, $intOff, $intLimit, &$intTotal=0) {
		try {	
			$this->prepare('call products_getall(?,?,?,?,?)');
			$this->bindParam(1, $intCate, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intSub, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intType, PDO::PARAM_INT, 4);
			$this->bindParam(4, $intOff, PDO::PARAM_INT, 11);
			$this->bindParam(5, $intLimit, PDO::PARAM_INT, 4);		
			$arrResult 	= $this->fetchAll();			
			$intTotal	= isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Product::getAll - ' .$e->getMessage());			
			return null;
		}
	}
	public function getBasket($intUserId, $intTargetId) {
		try {	
			$strKey 	= PRODUCT_GETBASKET . $intUserId . $intTargetId;
			$arrResult 	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call baskets_getall(?,?)');
				$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
				$this->bindParam(2, $intTargetId, PDO::PARAM_INT, 11);
				$arrResult = $this->fetchAll();
				$this->cache->set($strKey,$arrResult,PRODUCT_GETBASKET_EXPIRE);
			}
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Product::getBasket - ' .$e->getMessage());			
			return null;
		}
	}
	public function addBasket($intUserId, $intTargetId, $intProductId, $intPrice) {
		try {	
			$this->prepare('call baskets_addupdate(?,?,?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intTargetId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intProductId, PDO::PARAM_INT, 11);
			$this->bindParam(4, $intPrice, PDO::PARAM_INT, 11);	
			$arrResult	= $this->fetchAll();
			$intError 	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$this->clear->deleteBasket($intUserId, $intTargetId);
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Product::addBasket - ' .$e->getMessage());			
			return null;
		}
	}
	public function updateBasketQuantity($intUserId, $intBasketId, $intProductId, $intQuantity, $intPrice,&$intTargetId=0) {
		try {				
			$this->prepare('call baskets_update_quantity(?,?,?,?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intBasketId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intProductId, PDO::PARAM_INT, 11);
			$this->bindParam(4, $intQuantity, PDO::PARAM_INT, 6);
			$this->bindParam(5, $intPrice, PDO::PARAM_INT, 11);	
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : -1;
			if( $intError == 0 ) {
				$intTargetId = $arrResult[0]['targetid'];
				$this->clear->deleteBasket($intUserId, $intTargetId);				
			}
			return isset($arrResult[0]) ? $arrResult[0] : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Product::updateBasketQuantity - ' .$e->getMessage());			
			return null;
		}
	}
	public function deleteBasket($intUserId, $intTargetId) {
		try {	
			$this->prepare('call baskets_delete(?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intTargetId, PDO::PARAM_INT, 11);
			$intError = $this->execute();
			if($intError) {
				$this->clear->deleteBasket($intUserId, $intTargetId);
				return 0;
			} return 1;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Product::deleteBasket - ' .$e->getMessage());			
			return null;
		}
	}
	public function getBasketSta($intUserId, $intTargetId) {
		try {	
			$strKey 	= PRODUCT_GETBASKETSTA . $intUserId . $intTargetId;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call baskets_sta_get(?,?)');
				$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
				$this->bindParam(2, $intTargetId, PDO::PARAM_INT, 11);
				$arrResult = $this->fetchAll();
				$this->cache->set($strKey, $arrResult, PRODUCT_GETBASKETSTA_EXPIRE);
			}
			return isset($arrResult[0]) ? $arrResult[0] : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Product::getBasketSta - ' .$e->getMessage());			
			return null;
		}
	}
	public function checkoutBasket($intUserId, $intTargetId, $intFullhouseId=0) {
		try {	
			$this->prepare('call baskets_checkout(?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intTargetId, PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0; 
			$intType	= isset($arrResult[0]['type']) ? $arrResult[0]['type'] : 0;  
			if($intError == 0) {								
				if($intType == 1) {
					$objURequest 	= new URequest();
					$objNotify		= new Notification();
					$arrFortuneGift = $this->getFortuneGiftList($intTargetId,$intUserId);
					foreach($arrFortuneGift as $value) {
						$jsonMessage = array('productid'=>$value['productid'],'productname'=>$value['name'],'quantity'=>$value['quantity']);
						$strMessage	 = Zend_Json::encode($jsonMessage);
						$objURequest->addRequest($intTargetId,$intUserId,$strMessage,$value['fgiftid'],1);
						$strContent	 = Zend_Json::encode(array(	'posterid'=>$intUserId,
																'productid'=>$value['productid'],
																'productname'=>$value['name'],
																'quantity'=>Vfriend_Function::numberFormat($value['quantity']))
														);
						$objNotify->insert($intTargetId,24,$strContent,$value['fgiftid']);
					}
					$this->updateFortuneGift($intTargetId,$intUserId);
					$this->clear->deleteFortuneByUser($intTargetId);
				}	
				$this->clear->deleteBasket($intUserId, $intTargetId);								
				$this->clear->deleteFortuneByUser($intUserId);		
				if($intFullhouseId > 0 && $intTargetId == 0) {
					$this->clear->deleteAccount($intFullhouseId, 2);				
				} else {
					$this->clear->deleteAccount($intUserId);
				}		
				return 0;
			} 
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Product::checkoutBasket - ' .$e->getMessage());			
			return null;
		}
	}
	public function getFortuneGiftList($intUserId, $intTargetId) {
		try {	
			$this->prepare('call fortunes_gift_getall(?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intTargetId, PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();			
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Product::getFortuneGiftList - ' .$e->getMessage());			
			return null;
		}
	}
	public function updateFortuneGift($intUserId, $intTargetId) {
		try {	
			$this->prepare('call fortunes_gift_update(?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intTargetId, PDO::PARAM_INT, 11);
			return $this->execute();
		} catch(Exception $e) {
			Vfriend_Function::logDb('Product::updateFortuneGift - ' .$e->getMessage());			
			return null;
		}
	}
	public function getAllForFortune($intUserId, $intOffset, $intLimit,&$intTotal=0) {
		try {	
			$strKey		= '';
			$arrResult	= null;
			if($intOffset == 0) {
				$strKey 	= PRODUCT_GETALLFORFORTUNE . $intUserId;
				$arrResult	= $this->cache->get($strKey);
			}
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call fortunes_getall(?,?,?)');
				$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
				$this->bindParam(2, $intOffset, PDO::PARAM_INT, 11);
				$this->bindParam(3, $intLimit, PDO::PARAM_INT, 11);
				$arrResult 	= $this->fetchAll();
				if($strKey != '') {
					$this->cache->set($strKey, $arrResult, PRODUCT_GETALLFORFORTUNE_EXPIRE);
				}
			}
			$intTotal	= isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;			
			return $arrResult;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Product::getAllForFortune - ' .$e->getMessage());			
			return null;
		}
	}
	public function getAllForPropose($intUserId) {
		try {	
			$strKey 	= PRODUCT_GETALLFORPROPOSE . $intUserId;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call fortunes_getall_propose(?)');
				$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
				$arrResult 	= $this->fetchAll();
				$this->cache->set($strKey, $arrResult, PRODUCT_GETALLFORPROPOSE_EXPIRE);
			}		
			return $arrResult;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Product::getAllForPropose - ' .$e->getMessage());			
			return null;
		}
	}
	public function getAllForGift($intUserId, $intOff, $intLimit,&$intTotal=0) {
		try {	
			$strKey 	= '';
			$arrResult	= null;
			if($intOff == 0) {
				$strKey 	= PRODUCT_GETALLFORGIFT . $intUserId;
				$arrResult	= $this->cache->get($strKey);
			}
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call fortunes_gift_getall_user(?,?,?)');
				$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
				$this->bindParam(2, $intOff, PDO::PARAM_INT, 11);
				$this->bindParam(3, $intLimit, PDO::PARAM_INT, 4);
				$arrResult 	= $this->fetchAll();
				if($strKey != '') {
					$this->cache->set($strKey, $arrResult, PRODUCT_GETALLFORPROPOSE_EXPIRE);
				}
			}		
			$intTotal = isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return $arrResult;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Product::getAllForGift - ' .$e->getMessage());			
			return null;
		}
	}
	public function getAllForProduct($intUserId, $intProductId, $intOffset=0, $intLimit=FORTUNE_PRODUCT_LIMIT) {
		try {	
			$strKey 	= PRODUCT_GETALLFORPRODUCT . $intUserId . $intProductId;
			$arrResult	= $this->cache->get($strKey);			
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call fortunes_getall_productid(?,?,?,?)');
				$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
				$this->bindParam(2, $intProductId, PDO::PARAM_INT, 11);
				$this->bindParam(3, $intOffset, PDO::PARAM_INT, 11);
				$this->bindParam(4, $intLimit, PDO::PARAM_INT, 4);
				$arrData = $this->fetchAll();
				if($arrData) {			
					$intCount = count($arrData);	
					$intInd	  = 0;
					for($i=0;$i<$intCount;$i++) {
						$intQuantity 	= $arrData[$i]['quantity'];
						$intFortuneId 	= $arrData[$i]['fortuneid'];
						$intPrice 		= $arrData[$i]['price'];
						$intPriceSo 	= $arrData[$i]['price_so'];
						$intType	 	= $arrData[$i]['type'];
						$intStatuses	= $arrData[$i]['statuses'];
						$strExpired		= $arrData[$i]['expired'];
						$intExpired		= $arrData[$i]['expday'];
						$strPostdate	= $arrData[$i]['postdate'];
						$intSrcId		= $arrData[$i]['src_id'];
						$strName		= $arrData[$i]['name'];
						$strImage		= $arrData[$i]['image'];
						$strImageExp	= $arrData[$i]['image_exp'];
						$isExpired		= (strtotime($strExpired) > time()) ? 0 : 1;
						if($intQuantity > 1) {
							for($j=0;$j<$intQuantity;$j++) {
								$arrResult[] = array(
												'stt'			=> ++$intInd,
												'fortuneid'		=> $intFortuneId,
												'price'			=> $intPrice,
												'price_so'		=> $intPriceSo,
												'type'			=> $intType,
												'statuses'		=> $intStatuses,
												'expired'		=> $intExpired,
												'postdate'		=> $strPostdate,
												'src_id'		=> $intSrcId,
												'name'			=> $strName,
												'image'			=> $strImage,
												'image_exp'		=> $strImageExp,
												'is_expired'	=> $isExpired
											);	
							}
						} else {
							$arrResult[] = array(
												'stt'			=> ++$intInd,
												'fortuneid'		=> $intFortuneId,
												'price'			=> $intPrice,
												'price_so'		=> $intPriceSo,
												'type'			=> $intType,
												'statuses'		=> $intStatuses,
												'expired'		=> $intExpired,
												'postdate'		=> $strPostdate,
												'src_id'		=> $intSrcId,
												'name'			=> $strName,
												'image'			=> $strImage,
												'image_exp'		=> $strImageExp,
												'is_expired'	=> $isExpired
											);						
						}
					}	
					$this->cache->set($strKey,$arrResult,PRODUCT_GETALLFORPRODUCT_EXPIRE);
				}				
			}
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Product::getAllForFortune - ' .$e->getMessage());			
			return null;
		}
	}
	public function sellProduct($intUserId, $intProductId, $intFortuneId) {
		try {
			$this->prepare('call fortunes_sell(?,?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intProductId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intFortuneId, PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$intType	= isset($arrResult[0]['type']) ? $arrResult[0]['type'] : 0;
				$this->clear->deleteProductByUser($intUserId, $intProductId);
				$this->clear->deleteAccount($intUserId,$intType);
			}	
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Product::sellProduct - ' .$e->getMessage());			
			return null;
		}
	}
	public function deleteProduct($intUserId, $intProductId, $intFortuneId) {
		try {
			$this->prepare('call fortunes_delete(?,?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intProductId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intFortuneId, PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$this->clear->deleteProductByUser($intUserId, $intProductId);
			}	
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Product::deleteProduct - ' .$e->getMessage());			
			return null;
		}
	}
}
?>